package cc.vv.baselibrary.util;

import android.widget.ImageView;

import java.io.File;

import cc.vv.lkimagecomponent2.LKImage;
import cc.vv.lkimagecomponent2.loaderplugin.BitmapListener;
import cc.vv.lkimagecomponent2.loaderplugin.LoadConfig;
import cc.vv.lkimagecomponent2.loaderplugin.TargetViewCallback;

/**
 * Created by sun.li on 2018/5/7.
 *
 * @author sun.li
 */

public class BTImageUtil {


    public static void load(String url, BTImageLoadOption imageLoadAttribute, BitmapListener bitmapListener) {
        initImageLoadAttribute(imageLoadAttribute).load(url).asBitmap(bitmapListener);
    }

    public static void load(String url, BTImageLoadOption imageLoadAttribute, ImageView imageView) {
        initImageLoadAttribute(imageLoadAttribute).load(url).into(imageView);
    }

    public static void load(String url, BTImageLoadOption imageLoadAttribute, TargetViewCallback targetViewCallback) {
        initImageLoadAttribute(imageLoadAttribute).load(url).into(targetViewCallback);
    }


    public static void loadFile(File file, BTImageLoadOption imageLoadAttribute, BitmapListener bitmapListener) {
        initImageLoadAttribute(imageLoadAttribute).loadFile(file).asBitmap(bitmapListener);
    }

    public static void loadFile(File file, BTImageLoadOption imageLoadAttribute, ImageView imageView) {
        initImageLoadAttribute(imageLoadAttribute).loadFile(file).into(imageView);
    }

    public static void loadFile(File file, BTImageLoadOption imageLoadAttribute, TargetViewCallback targetViewCallback) {
        initImageLoadAttribute(imageLoadAttribute).loadFile(file).into(targetViewCallback);
    }


    public static void loadFile(String filePath, BTImageLoadOption imageLoadAttribute, BitmapListener bitmapListener) {
        initImageLoadAttribute(imageLoadAttribute).loadFile(filePath).asBitmap(bitmapListener);
    }

    public static void loadFile(String filePath, BTImageLoadOption imageLoadAttribute, ImageView imageView) {
        initImageLoadAttribute(imageLoadAttribute).loadFile(filePath).into(imageView);
    }

    public static void loadFile(String filePath, BTImageLoadOption imageLoadAttribute, TargetViewCallback targetViewCallback) {
        initImageLoadAttribute(imageLoadAttribute).loadFile(filePath).into(targetViewCallback);
    }


    public static void loadRes(int resId, BTImageLoadOption imageLoadAttribute, BitmapListener bitmapListener) {
        initImageLoadAttribute(imageLoadAttribute).loadRes(resId).asBitmap(bitmapListener);
    }

    public static void loadRes(int resId, BTImageLoadOption imageLoadAttribute, ImageView imageView) {
        initImageLoadAttribute(imageLoadAttribute).loadRes(resId).into(imageView);
    }

    public static void loadRes(int resId, BTImageLoadOption imageLoadAttribute, TargetViewCallback targetViewCallback) {
        initImageLoadAttribute(imageLoadAttribute).loadRes(resId).into(targetViewCallback);
    }


    public static void loadAsserts(String assertsPath, BTImageLoadOption imageLoadAttribute, BitmapListener bitmapListener) {
        initImageLoadAttribute(imageLoadAttribute).loadAsserts(assertsPath).asBitmap(bitmapListener);
    }

    public static void loadAsserts(String assertsPath, BTImageLoadOption imageLoadAttribute, ImageView imageView) {
        initImageLoadAttribute(imageLoadAttribute).loadAsserts(assertsPath).into(imageView);
    }

    public static void loadAsserts(String assertsPath, BTImageLoadOption imageLoadAttribute, TargetViewCallback targetViewCallback) {
        initImageLoadAttribute(imageLoadAttribute).loadAsserts(assertsPath).into(targetViewCallback);
    }

    public static void loadRaw(int rawId, BTImageLoadOption imageLoadAttribute, BitmapListener bitmapListener) {
        initImageLoadAttribute(imageLoadAttribute).loadRaw(rawId).asBitmap(bitmapListener);
    }

    public static void loadRaw(int rawId, BTImageLoadOption imageLoadAttribute, ImageView imageView) {
        initImageLoadAttribute(imageLoadAttribute).loadRaw(rawId).into(imageView);
    }

    public static void loadRaw(int rawId, BTImageLoadOption imageLoadAttribute, TargetViewCallback targetViewCallback) {
        initImageLoadAttribute(imageLoadAttribute).loadRaw(rawId).into(targetViewCallback);
    }

    /**
     * 初始化图片加载属性
     */
    private static LoadConfig.ConfigBuilder initImageLoadAttribute(BTImageLoadOption imageLoadOption) {
        LoadConfig.ConfigBuilder configBuilder = LKImage.load();

        if (imageLoadOption == null) {
            return configBuilder;
        }

        configBuilder.animate(imageLoadOption.getAnimation());
        configBuilder.animate(imageLoadOption.getAnimationId());
        configBuilder.gifRepeatCount(imageLoadOption.getGifRepeatCount());
        configBuilder.placeHolder(imageLoadOption.getPlaceHolderResId());
        configBuilder.placeHolder(imageLoadOption.getPlaceHolderDrawable());
        configBuilder.error(imageLoadOption.getErrorResId());
        configBuilder.error(imageLoadOption.getErrorDrawable());
        configBuilder.thumbnail(imageLoadOption.getThumbnail());
        configBuilder.override(imageLoadOption.getoWidth(), imageLoadOption.getoHeight());


        if (imageLoadOption.getShapeMode() != null) {
            switch (imageLoadOption.getShapeMode()) {
                case RECT_ROUND:
                    configBuilder.rectRoundCorner(imageLoadOption.getRectRoundRadius());
                    break;
                case OVAL:
                    configBuilder.asCircle();
                    break;
                case SQUARE:
                    configBuilder.asSquare();
                    break;
                case RECT:
                default:
                    break;
            }
        }

        if (imageLoadOption.isNeedFilteColor()) {
            configBuilder.colorFilter(imageLoadOption.getFilteColor());
        }
        if (imageLoadOption.isNeedBrightness()) {
            configBuilder.brightnessFilter(imageLoadOption.getBrightnessLeve());
        }
        if (imageLoadOption.isNeedContrast()) {
            configBuilder.contrastFilter(imageLoadOption.getContrastLevel());
        }
        if (imageLoadOption.isNeedInvert()) {
            configBuilder.invertFilter();
        }
        if (imageLoadOption.isNeedPixelation()) {
            configBuilder.pixelationFilter(imageLoadOption.getPixelationLevel());
        }
        if (imageLoadOption.isNeedSepia()) {
            configBuilder.sepiaFilter();
        }
        if (imageLoadOption.isNeedSketch()) {
            configBuilder.sketchFilter();
        }
        if (imageLoadOption.isNeedSwirl()) {
            configBuilder.swirlFilter();
        }
        if (imageLoadOption.isNeedToon()) {
            configBuilder.toonFilter();
        }
        if (imageLoadOption.isNeedVignette()) {
            configBuilder.vignetteFilter();
        }
        if (imageLoadOption.isNeedGrayscale()) {
            configBuilder.grayscaleFilter();
        }
        if (imageLoadOption.isNeedBlur()) {
            configBuilder.blur(imageLoadOption.getBlurRadius());
        }

        if (imageLoadOption.isCrossFade()) {
            if (imageLoadOption.getCrossFadeDuration() != 0) {
                configBuilder.crossFade(imageLoadOption.getCrossFadeDuration());
            } else {
                configBuilder.crossFade();
            }
        }

        if (imageLoadOption.getPriority() != null) {
            configBuilder.priority(imageLoadOption.getPriority());
        }
        if (imageLoadOption.getDiskCacheStrategy() != null) {
            configBuilder.diskCacheStrategy(imageLoadOption.getDiskCacheStrategy());
        }
        if (imageLoadOption.getScaleMode() != null) {
            configBuilder.scale(imageLoadOption.getScaleMode());
        }

        if (!imageLoadOption.isAnim()) {
            configBuilder.dontAnim();
        }
        return configBuilder;
    }
}
